<?php

namespace App\HttpController\Api\Attend\Student;

use App\HttpController\Api\Admin\AdminBase;
use EasySwoole\Http\Message\Status;
use App\Model\Attend\Process as ProcessModel;
use App\Model\Face\Department as DepartmentModel;
use App\Model\Face\Face as FaceModel;
use App\Lib\Utils;

class Process extends AdminBase
{

    public function index()
    {

    }

    /**
     * 获取权限列表
     * @return [type] [description]
     */
    public function lists(){
    	$where = [];
        if(isset($this->params['name']) && !empty($this->params['name'])) {
            $where['name'] = ['%'.$this->params['name'].'%','like'];
        }
        $where['companyid'] = $this->userinfo['companyid'];
        try {
            //查询审批流程数据
            $field = '`id`,`name`,`type`,`objid`,`authto`,`sendto`,`minday`,`maxday`,`createtime`';
            $processObj = new processModel();
            $data = $processObj->getPageList($field, $where, $this->params['page'], $this->params['limit']);
            //查询所有部门，
            $depObj = new DepartmentModel();
            $depArr = $depObj->getIDName(['companyid'=>$this->userinfo['companyid']]);

            //查询人员
            $faceObj = new FaceModel();
            $staff_where = [];
            $staff_where['status'] = 1;
            $staff_where['mode'] = 2;
            $staff_where['companyid'] = $this->userinfo['companyid'];
            $staff = $faceObj->getIDName($staff_where);
        }catch(\Exception $e) {
             echo $e->getMessage();
            return $this->writeJson(Status::CODE_BAD_REQUEST, "Server Error");
        }
        
        if(!empty($data['lists'])) {
            $leaderArr = ['1'=>'直接主管','2'=>'二级主管','3'=>'三级主管','4'=>'四级主管','5'=>'五级主管','6'=>'六级主管','7'=>'七级主管','8'=>'八级主管'];
            foreach($data['lists'] as &$list) {
                $objid = explode(',', $list['objid']);
                $objStr = '';
                if($list['type'] == 1){
                    $list['typeName'] = '员工';
                    //特殊人员请假审核
                    //部门请假审核
                    foreach ($objid as $key => $value) {
                        if(isset($staff[$value])){
                            $objStr .= ','.$staff[$value];
                        }
                    }
                    
                }else{
                    $list['typeName'] = '部门';
                    //部门请假审核
                    foreach ($objid as $key => $value) {
                        if(isset($depArr[$value])){
                            $objStr .= ','.$depArr[$value];
                        }
                    }
                }
                $list['objStr'] = trim($objStr, ',');
                $list['objArr'] = $objid;
                //显示审核人
                $authto = json_decode($list['authto'], true);
                $authtoStr = '';
                $authtoName = [];
                foreach ($authto as $key => $value) {
                    $authArr = explode('-', $value);
                    $authType = reset($authArr);
                    $authid = end($authArr);
                    if($authType == 1){
                        if(isset($leaderArr[$authid])){
                            $authtoStr .= '->'.$leaderArr[$authid];
                            $authtoName[] = $leaderArr[$authid];
                        }
                    }else{
                        if(isset($staff[$authid])){
                            $authtoStr .= '->'.$staff[$authid];
                            $authtoName[] = $staff[$authid];
                        }
                    }
                }
                $list['authtoStr'] = trim($authtoStr, '->');
                $list['authtoArr'] = $authto;
                $list['authtoName'] = $authtoName;
                //显示抄送人
                $sendto = explode(',', $list['sendto']);
                $sendtoStr = '';
                foreach ($sendto as $key => $value) {
                    if(isset($staff[$value])){
                        $sendtoStr .= ','.$staff[$value];
                    }
                }
                $list['sendtoStr'] = trim($sendtoStr, ',');
                $list['sendtoArr'] = $sendto;

                $list['createtime'] = date("Y-m-d H:i:s", $list['createtime']);
            }
        }

        return $this->writeJson(Status::CODE_LIST_OK, "ok", $data); 
    }



    /**
     * 添加班次
     */
    public function add(){
        if(isset($this->params['name']) && !empty($this->params['name'])){
            $data['name'] = $this->params['name'];
        }else{
            return $this->writeJson(Status::CODE_TIP, "请输入审批流名称");
        }
        if(isset($this->params['minday']) && !empty($this->params['minday'])){
            $data['minday'] = $this->params['minday'];
        }else{
            $data['minday'] = 0;
        }
        if(isset($this->params['maxday']) && !empty($this->params['maxday'])){
            $data['maxday'] = $this->params['maxday'];
        }else{
            return $this->writeJson(Status::CODE_TIP, "请输入最大天数");
        }

        if(isset($this->params['objid']) && !empty($this->params['objid'])){
            $data['objid'] = ','.trim($this->params['objid'], ',').',';
        }else{
            return $this->writeJson(Status::CODE_TIP, "请选择被审批对象");
        }
        if(isset($this->params['auth']) && !empty($this->params['auth'])){
            $auth = $this->params['auth'];
            $data['authto'] = json_encode($auth);
        }else{
            return $this->writeJson(Status::CODE_TIP, "请选择审批人");
        }
        if(isset($this->params['sendto']) && !empty($this->params['sendto'])){
            $data['sendto'] = $this->params['sendto'];
        }

        // 插入
        try {
            $processObj = new ProcessModel();

            // $where = [];
            // $where['companyid'] = $this->userinfo['companyid'];
            // $where['name'] = $data['name']; 

            // $isset = $leaveObj->getOne('id', $where);

            // if($isset && is_array($isset)){
            //     return $this->writeJson(Status::CODE_LIST_OK, '假期已存在', []);
            // }
            
            $data['createtime'] = time();
            $data['status'] = 1;
            $data['userid'] = $this->userinfo['userid'];
            $data['parentid'] = $this->userinfo['parentid'];
            $data['parentpath'] = $this->userinfo['parentpath'];
            $data['companyid'] = $this->userinfo['companyid'];

            $processid = $processObj->add($data);

        }catch(\Exception $e) {
            return $this->writeJson(Status::CODE_BAD_REQUEST, "Server Error");
        }
        
        if(!empty($processid)) {
            return $this->writeJson(Status::CODE_OK, 'OK', ['id' => $processid ]);
        } else {
            return $this->writeJson(Status::CODE_BAD_REQUEST, '操作数据失败', ['id' => 0 ]);
        }
    }

    /**
     * 修改请假审批流程
     * @return [type] [description]
     */
    public function edit(){
        if(isset($this->params['id']) && !empty($this->params['id'])){
            $id = $this->params['id'];
        }else{
            return $this->writeJson(Status::CODE_TIP, '操作数据失败', ['id' => 0 ]);
        }
        
        if(isset($this->params['name']) && !empty($this->params['name'])){
            $data['name'] = $this->params['name'];
        }else{
            return $this->writeJson(Status::CODE_TIP, "请输入审批流名称");
        }
        if(isset($this->params['minday']) && !empty($this->params['minday'])){
            $data['minday'] = $this->params['minday'];
        }else{
            $data['minday'] = 0;
        }
        if(isset($this->params['maxday']) && !empty($this->params['maxday'])){
            $data['maxday'] = $this->params['maxday'];
        }else{
            return $this->writeJson(Status::CODE_TIP, "请输入最大天数");
        }
        if(isset($this->params['objid']) && !empty($this->params['objid'])){
            $data['objid'] = ','.trim($this->params['objid'], ',').',';
        }else{
            return $this->writeJson(Status::CODE_TIP, "请选择被审批对象");
        }
        if(isset($this->params['auth']) && !empty($this->params['auth'])){
            $auth = $this->params['auth'];
            $data['authto'] = json_encode($auth);
        }else{
            return $this->writeJson(Status::CODE_TIP, "请选择审批人");
        }
        if(isset($this->params['sendto']) && !empty($this->params['sendto'])){
            $data['sendto'] = $this->params['sendto'];
        }


        // 插入
        try {
            $processObj = new ProcessModel();

            $up_where = [];
            $up_where['id'] = $id;

            $result = $processObj->update($data,$up_where);

        }catch(\Exception $e) {
            echo $e->getMessage();
            return $this->writeJson(Status::CODE_BAD_REQUEST, "Server Error");
        }
        
        if($result) {
            return $this->writeJson(Status::CODE_OK, 'OK', ['id' => $id ]);
        } else {
            return $this->writeJson(Status::CODE_BAD_REQUEST, '操作数据失败', ['id' => 0 ]);
        }
    }


    /**
     * 删除权限信息
     * @return [type] [description]
     */
    public function delete(){
        //修改
        if(isset($this->params['id']) && !empty($this->params['id'])){
            $id = $this->params['id'];
        }else{
            return $this->writeJson(Status::CODE_TIP, '删除数据失败', []);
        }
        
        try {
            $processObj = new ProcessModel();
            $result = $processObj->destroy($id);
        }catch(\Exception $e) {
            // echo $e->getMessage();
            return $this->writeJson(Status::CODE_BAD_REQUEST, "Server Error");
        }

        if($result) {
            return $this->writeJson(Status::CODE_OK, 'OK', []);
        } else {
            return $this->writeJson(Status::CODE_TIP, '删除数据失败', []);
        }
    }


    public function delAll(){
        $ids = $this->params['ids'];

        try {
            $leaveObj = new LeaveModel();
            $result = $leaveObj->destroy($ids);
        }catch(\Exception $e) {
            // echo $e->getMessage();
            return $this->writeJson(Status::CODE_BAD_REQUEST, "Server Error");
        }

        if($result) {
            return $this->writeJson(Status::CODE_OK, 'OK', []);
        } else {
            return $this->writeJson(Status::CODE_BAD_REQUEST, 'FAIL', []);
        }
    } 


    /**
     * 查询考勤人员，用于无需考勤人员、考勤负责人
     * @return [type] [description]
     */
    public function staffTree()
    {
        try{
            $faceObj = new FaceModel();
            $depObj = new DepartmentModel();
            $where = [];
            $where['companyid'] = $this->userinfo['companyid'];
            $where['status'] = 1;
            
            $staff = $faceObj->getAll('id,name,departmentid as pid',$where);
            $department = $depObj->getAll('id,name,pid',$where);
            //将部门进行子集归类
            $data = Utils::list_to_tree($department);
            //将人员进行部门归类
            $data = Utils::depSetStaff($data, $staff);
            //重新排序
            $return = [];
            foreach ($data as $key => $value) {
                $return[] = $value;
            }

        }catch(\Exception $e){
            return $this->writeJson(Status::CODE_BAD_REQUEST,'Server Error');
        }

        if($return){
            return $this->writeJson(Status::CODE_OK, 'OK', $return);
        }else{
            return $this->writeJson(Status::CODE_TIP, 'FAIL', []);
        }
    }

    /**
     * 查询考勤人员，用于无需考勤人员、考勤负责人
     * @return [type] [description]
     */
    public function staffRadio(){

        try{
            $faceObj = new FaceModel();
            $depObj = new DepartmentModel();
            $where = [];
            $where['companyid'] = $this->userinfo['companyid'];
            $where['status'] = 1;
            
            $staff = $faceObj->getAll('id,name,departmentid as pid',$where);
            $department = $depObj->getAll('id,name,pid',$where);

            // var_dump($staff);
            // var_dump($department);
            $data = Utils::list_to_tree($department);

            $data = Utils::depSetStaff($data, $staff);
            $return = [];
            foreach ($data as $key => $value) {
                if(isset($value['children'])){
                    $value['disabled'] = true;
                }
                $return[] = $value;
            }

        }catch(\Exception $e){
            return $this->writeJson(Status::CODE_BAD_REQUEST,'Server Error');
        }

        if($return){
            return $this->writeJson(Status::CODE_OK, 'OK', $return);
        }else{
            return $this->writeJson(Status::CODE_TIP, 'FAIL', []);
        }
    }


    /**
     * 获取部门数据
     * @return [type] [description]
     */
    public function department(){
        if(isset($this->params['id']) && !empty($this->params['id'])){
            $id = $this->params['id'];
        }else{
            $id = 0;
        }
        try{
            $depObj = new DepartmentModel();
            $where = [];
            $where['companyid'] = $this->userinfo['companyid'];
            $data = $depObj->getAll('id,name,pid',$where);

            $result = Utils::list_to_tree($data);
        }catch(\Exception $e){
            return $this->writeJson(Status::CODE_BAD_REQUEST,'Server Error');
        }

        if($result){
            return $this->writeJson(Status::CODE_OK, 'OK', $result);
        }else{
            return $this->writeJson(Status::CODE_BAD_REQUEST, 'FAIL', []);
        }
    }

}
